package com.eastjet.maintenance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.maintenance.domain.Acceptance;
import com.eastjet.maintenance.domain.Annex;
import com.eastjet.maintenance.domain.Documentation;
import com.eastjet.maintenance.domain.Purchasedetails;
import com.eastjet.maintenance.feign.CodeDataFeing;
import com.eastjet.maintenance.mapper.AcceptanceMapper;
import com.eastjet.maintenance.service.AcceptanceService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 杨博文
 * @description 针对表【acceptance(到货验收)】的数据库操作Service实现
 * @createDate 2024-11-25 09:23:37
 */
@Service
public class AcceptanceServiceImpl extends ServiceImpl<AcceptanceMapper, Acceptance> implements AcceptanceService {

    @Autowired
    private AnnexServiceImpl annexServiceImpl;// 附件服务实现类
    @Autowired
    private PurchasedetailsServiceImpl purchasedetailsServiceImpl;// 采购单明细服务实现类
    @Autowired
    private DocumentationServiceImpl documentationServiceImpl;// 文档服务实现类
    @Autowired
    private CodeDataFeing codeDataFeing;// 编码自动生成接口
    @Autowired
    AcceptanceMapper acceptanceMapper;// 到货验收Mapper

    /*
     * 查询验收信息
     */
    @Override
    public List<Acceptance> SelectByAcceptanceId(Acceptance acceptance) {
        return acceptanceMapper.SelectByAcceptanceId(acceptance);
    }

    @Override
    public Boolean UpdateAcceptance(List<Acceptance> acceptance) {
        return null;
    }

    /*
     * 新增验收信息
     */
    @SneakyThrows
    @Override
    public Boolean InsertAcceptance(Map<String, Object> map) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String code = codeDataFeing.test(new HashMap<String, String>() {{
            put("TableName", "acceptance");
        }});
        Acceptance acceptance = new Acceptance();// 实例化到货验收对象
        acceptance.setCode(code);
        acceptance.setTopic(map.get("topic").toString());
        acceptance.setDate(df.parse(map.get("date").toString()));
        acceptance.setAcceptor(map.get("acceptor").toString());
        acceptance.setDepartment(map.get("department").toString());
        acceptance.setPurchase(map.get("purchase").toString());
        acceptance.setState(0);
        acceptance.setRemark(map.get("remark").toString());
        acceptance.setCreatedBy(map.get("acceptor").toString());
        acceptance.setCreationTime(df.parse(df.format(new Date())));
        acceptance.setIs_delete(0);
        boolean save = this.save(acceptance);
        if (save) {
            // 修改采购明细
            List<Map<String, Object>> tableData3 = (List<Map<String, Object>>) map.get("tableData3");
            // 新增文档
            List<Map<String, Object>> documents = (List<Map<String, Object>>) map.get("documents");
            List<Purchasedetails> ps = new ArrayList<Purchasedetails>();// 采购单明细对象集合
            List<Annex> annexes = new ArrayList<Annex>();// 附件对象集合
            List<Documentation> documentations = new ArrayList<Documentation>();// 文档对象集合
            // 循环映射文档对象
            documents.forEach(item -> {
                item = (Map<String, Object>) item;
                Documentation documentation = new Documentation();
                documentation.setCode(code);
                documentation.setFilename(((Map<?, ?>) item).get("name").toString());
                try {
                    documentation.setUploadTime(df.parse(((Map<?, ?>) item).get("uploadDate").toString()));
                    documentation.setCreationTime(df.parse(((Map<?, ?>) item).get("uploadDate").toString()));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                documentation.setUploader(((Map<?, ?>) item).get("uploader").toString());
                documentation.setSize(((Map<?, ?>) item).get("size").toString());
                documentation.setCreatedBy(((Map<?, ?>) item).get("uploader").toString());
                documentations.add(documentation);
            });
            //循环映射采购单明细对象/附件对象
            tableData3.forEach(item -> {
                List<Object> list = (List<Object>) item.get("listData");
                List<Object> list2 = (List<Object>) item.get("tableData");
                //循环映射采购单明细对象
                list.forEach(item2 -> {
                    item2 = (Map<String, Object>) item2;
                    Purchasedetails p = new Purchasedetails();
                    p.setId(Integer.valueOf(((Map<?, ?>) item2).get("id").toString()));
                    p.setName(((Map<?, ?>) item2).get("name").toString());
                    p.setModel(((Map<?, ?>) item2).get("model").toString());
                    p.setType(((Map<?, ?>) item2).get("type").toString());
                    p.setManufacture(((Map<?, ?>) item2).get("manufacture").toString());
                    p.setManufacturer(((Map<?, ?>) item2).get("manufacturer").toString());
                    p.setBrand(((Map<?, ?>) item2).get("brand").toString());
                    p.setUseDepartments(((Map<?, ?>) item2).get("useDepartments").toString());
                    p.setFactoryNumber(((Map<?, ?>) item2).get("factoryNumber").toString());
                    p.setUseDepartments(((Map<?, ?>) item2).get("useDepartments").toString());
                    p.setTheEquipment(((Map<?, ?>) item2).get("theEquipment").toString());
                    p.setWeight(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item2).get("weight").toString())));
                    p.setActualPrice(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item2).get("actualPrice").toString())));
                    p.setActual(Integer.valueOf(((Map<?, ?>) item2).get("actual").toString()));
                    p.setActualTotal(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item2).get("actualTotal").toString())));
                    p.setJieg(((Map<?, ?>) item2).get("jieg").toString());
                    p.setTotalPower(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item2).get("totalPower").toString())));
                    p.setDaohuo(Integer.valueOf(((Map<?, ?>) item2).get("daohuo").toString()));
                    p.setWeidaohuo(Integer.valueOf(((Map<?, ?>) item2).get("weidaohuo").toString()));
                    p.setBenci(Integer.valueOf(((Map<?, ?>) item2).get("benci").toString()));
                    try {
                        p.setDaoqi(df.parse(((Map<?, ?>) item2).get("daoqi").toString()));
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    p.setQingk(((Map<?, ?>) item2).get("qingk").toString());
                    p.setRemark(((Map<?, ?>) item2).get("remark").toString());
                    ps.add(p);
                });
                //循环映射附件对象
                list2.forEach(item3 -> {
                    item3 = (Map<String, Object>) item3;
                    Annex annex = new Annex();
                    annex.setCode(code);
                    annex.setNumbering(code + "fjian");
                    annex.setName(((Map<?, ?>) item3).get("name").toString());
                    annex.setModel(((Map<?, ?>) item3).get("model").toString());
                    annex.setType(((Map<?, ?>) item3).get("type").toString());
                    annex.setQuantity(Integer.valueOf(((Map<?, ?>) item3).get("quantity").toString()));
                    annex.setRemark(((Map<?, ?>) item3).get("remark").toString());
                    annexes.add(annex);
                });
            });
            Boolean b = purchasedetailsServiceImpl.UpdatePurchasedetails(ps);
            if (b) {
                // 新增附件
                Boolean b1 = annexServiceImpl.InsertAnnex(annexes);
                if (b1) {
                    return documentationServiceImpl.InsertList(documentations);
                }
            }
        }
        return false;
    }
}




